<?php
namespace app\common\logic\cast\news;



use app\common\logic\cast\Base;
use app\common\utils\Utils;
use fast\Http;
use fast\Random;
use think\exception\ValidateException;


/**
 * Created by PhpStorm.
 * User: yangchunfu
 * Date: 2019/12/24
 * Time: 4:36 PM
 * 栏目
 */

class Dyvideo extends Base
{
    protected $apiHref = 'https://api03.6bqb.com/xigua/app/categoryVideo';
    private $apikey = '71BC2571794B8849E2F37CBCF2014BF4';
    private $cates = [
        'subv_xg_movie',
        'subv_variety',
        'subv_video_food',
        'subv_xg_nba',
        'subv_xg_funny',
        'subv_sport',
        'video_new',
        'subv_car',
        'subv_xg_entertainment',
        'subv_video_culture',
        'subv_xg_vlog',
        'subv_video_tech'
    ];
    public function __construct($options= [], $tag = '')
    {
        $this->apiHref = config('site.videoconfig');
        $k = array_rand($this->cates, 1);
        $cate_id = $this->cates[$k];
        $options = array_merge([
            'apikey' => $this->apikey,
            'category' => $cate_id
        ], $options);

        parent::__construct($options, $tag);
    }

    /**
     * 获取视频链接
     * id,url,title,publishDate,imageUrls
     */
    public function getField($v) {
        if (isset($v['content']) && $v['content']) {
            $v['content'] = json_decode($v['content'], true);
            if ($v['content']['abstract']) {
                $data['title'] = $v['content']['abstract'];
            } else {
                return [];
            }
            if ($v['content']['article_url']) {
                $data['url'] = $v['content']['article_url'];
            } else {
                return [];
            }
            if ($v['content']['video_detail_info']) {
                $detail = $v['content']['video_detail_info'];
                $image = $detail['detail_video_large_image']['url'];
                $data['viewCount'] = isset($detail['video_watch_count']) ? $detail['video_watch_count'] : 0;
                $data['imageUrls'] = $image;
            } else {
                return [];
            }

            $data['news_id'] = (isset($v['content']['id']) && $v['content']['id']) ? $v['content']['id'] : Random::uuid(); //根据时间戳
            $data['publishDate'] = (isset($v['content']['publish_time']) && $v['content']['publish_time']) ? $v['content']['publish_time'] : time();
            $data['commentCount'] = isset($v['content']['comment_count']) ? $v['content']['comment_count'] : 0;
            $data['likeCount'] = isset($v['content']['digg_count']) ? $v['content']['digg_count'] : 0;
            return $data;
        } else {
            throw new ValidateException('过滤去除');
        }
    }


    /**
     * 调用接口
     * @param mixed $options
     */
    public function get($options = []) {
        $switch1 = config('site.xiguaswitch');
        $switch2 = config('site.bilibiswitch');
        $res1 = [];
        $res2 = [];
        if ($switch1 == 1) {
           $res1 = parent::get();
        }

        if ($switch2 == 1) {
            //把bilibli视频放在西瓜目录下
            $res2 = (new Bilibi([], 'dyvideo'))->get();
        }

        $data = array_merge($res1, $res2);
        return $data;
    }
}

